<template>
	<view>
		<!-- 选择收货地址的盒子 -->
		<view class="address-choose-box" v-if="JSON.stringify(address) === '{}'" @click="chooseAddress">
			<button type="primary" size="mini" class="btnChooseAddress">请选择收货地址 + </button>
		</view>
		
		<!-- 渲染收货信息的盒子 -->
		<view class="address-info-box" v-else @click="chooseAddress">
			<view class="row1">
				<view class="row1-left">
					<view class="username">收货人：{{address.userName}}</view>
				</view>
				<view class="row1-right">
					<view class="phone">电话：{{address.telNumber}}</view>
					<uni-icons type="arrowright" size="16"></uni-icons>
				</view>
			</view>
			<view class="row2">
				<view class="row2-left">收货地址：</view>
				<view class="row2-right">
					{{addstr}}
				</view>
			</view>
		</view>
		
		<!-- 底部分界线 -->
		<image src="/static/cart_border@2x.png" class="address-border"> </image>
	</view>
</template>

<script>
	// 导入vuex的辅助包
	import { mapState, mapMutations, mapGetters} from 'vuex'
	
	export default {
		name:"my-address",
		data() {
			return {
				// 收货地址显示与隐藏---在用户信息vuex中已经进行定义这里可以清除
				// address: {},
			};
		},
		methods: {
			// 调用用户信息vuex中的updateAddress()函数映射到当前页面使用
			...mapMutations('m_user', ['updateAddress']),
			// 选择收货地址---调用小程序的chooseAddress()API 
			async chooseAddress() {
				const [err, succ] = await uni.chooseAddress().catch(err => err)
				// 判断用户点击确定事件
				if (err === null && succ.errMsg === 'chooseAddress:ok') {
					// 直接将address对象数据传递到用户信息vuex进行存储
					this.updateAddress(succ)
				}
				// 判断用户点击取消事件
				if (err && err.errMsg === 'chooseAddress:fail cancel' || err.errMsg === 'chooseAddress:fail authorize no response') {
					// 调用this.reAuth()方法向用户重新发起授权
					this.reAuth()
				}
			},
			// 让用户重新授权收货地址
			async reAuth() {
				// 提示用户对地址进行授权
				const [err2, confirmResult] = await uni.showModal({
					content: '检测到您没打开地址权限，是否去设置打开？',
					confirmText: "确认",
					cancelText: "取消",
				})
				// 如果弹出异常直接退出
				if(err2) return
				
				// 如果点击了'取消'
				if (confirmResult.cancel) return uni.$showMsg('您取消了地址授权')
				
				// 如果点击了'确认'
				if (confirmResult.confirm) return uni.openSetting({
					// 对授权结果进行判断并提示
					success: (settingResult) => {
						if (settingResult.authSetting['scope.address']) return uni.$showMsg('授权成功 请选择地址')
						if (!settingResult.authSetting['scope.address']) return uni.$showMsg('授权失败')
					} 
				})
			},
		},
		computed: {
			// 将用户信息vuex中的address对象映射到当前页面使用---替换掉data中address对象
			...mapState('m_user', ['address']),
			
			// 渲染收货地址需要进行拼接---为了提高复用，将其迁移到用户vuex的getters中，这里直接调用即可
			// addstr() {
			// 	if(!this.address.provinceName) return ''
			// 	return this.address.provinceName + this.address.cityName + this.address.countyName + this.address.detailInfo
			// }
			...mapGetters('m_user', ['addstr']), 
 		}
	}
</script>

<style lang="scss">
.address-choose-box {
	height: 90px;
	display: flex;
	justify-content: center;
	align-items: center;
}

.address-info-box { 
	font-size: 12px;
	height: 90px;
	display: flex;
	flex-direction: column;
	justify-content: center;
	padding: 0 5px;
	.row1 {
		display: flex;
		justify-content: space-between;
		.row1-right {
			display: flex;
			justify-content: space-between;
		}
	}
	.row2 { 
		display: flex;
		// justify-content: space-between;
		align-items: center;
		margin-top: 10px;
		.row2-left {
			// 文本不换行
			white-space: nowrap;
		}
	}
}

.address-border {
	display: block;
	width: 100%;
	height: 5px;
}
</style>
